package cloudspace.stats.achieve;

import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericComposer;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Listbox;

import cloudspace.security.CloudSpaceUser;


public class AchieveControler extends GenericComposer
{
    /**
	 * 
	 */
    private static final long serialVersionUID = 753842710122033635L;

    Component mainWindow;


    private void init( String user )
    {
        ListModel achModel = new AchieveModel( user );
        Listbox box = (Listbox)mainWindow.getFellow( "Achieves" );
        box.setModel( achModel );
        box.setItemRenderer( new AchieveRenderer() );
    }


    public void doAfterCompose( Component comp ) throws Exception
    {
        mainWindow = comp;
        Object principal = SecurityContextHolder.getContext()
            .getAuthentication()
            .getPrincipal();
        String userName = null;
        if ( principal instanceof CloudSpaceUser )
        {
            userName = ( (CloudSpaceUser)principal ).getUsername();
        }
        else
        {
            return;
        }
        init( userName );
        // init("Select a User");
        // Combobox userSelector = (Combobox) comp.getFellow("userContext");
        // userSelector.setReadonly(true);
        // userSelector.setRawValue("Select a User");
        /*
         * Set<String> userNames =
         * StatisticsService.getStatService().getUsers(); for (String username :
         * userNames) { Comboitem item = new Comboitem();
         * item.setLabel(username); item.setValue(username);
         * item.setParent(userSelector);
         * 
         * }
         */
        /*
         * userSelector.addEventListener("onChange", new EventListener() {
         * 
         * @Override public void onEvent(Event event) throws Exception {
         * Combobox box = (Combobox)event.getTarget();
         * init((String)box.getSelectedItem().getValue()); }
         * 
         * });
         */

    }
}
